package info.xiao.dataStructrue.array;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname FindDuplicates
 * @Description 数组中重复的数据
 * @Url https://leetcode.cn/problems/find-all-duplicates-in-an-array/
 * @Date 2023/4/3 22:26
 * @Created by 晓龙Oba
 */
public class FindDuplicates {
    public static void main(String[] args) {
        int[] nums = {1, 1, 2};
        List<Integer> list = findDuplicates(nums);
        System.out.println(list);
    }

    private static List<Integer> findDuplicates(int[] nums) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[(Math.abs(nums[i]) - 1)] > 0) {
                nums[(Math.abs(nums[i]) - 1)] = 0 - nums[(Math.abs(nums[i]) - 1)];
                continue;
            }
            list.add(Math.abs(nums[i]));
        }
        return list;
    }
}
